package com.wxd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.annotation.DictFilter;
import com.wxd.modules.sys.dao.SysDictDao;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.entity.SysDictEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.modules.sys.service.SysDictService;
import com.wxd.utils.Constant;
import com.wxd.utils.PageUtils;
import com.wxd.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {
   @Autowired
   private SysDeptService sysDeptService;
    @Override
    @DictFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        String deptIdStr= (String) params.get("deptId");
        Long deptIdLong=null;
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            //根据部门对字典进行查看
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            //得到父部门字典,将父字典也展示出来
            List<Long> allParentIdList = sysDeptService.queryParentIdList(deptIdLong);
            deptIdList.addAll(allParentIdList);
        }

        IPage<SysDictEntity> page = this.page(
                new Query<SysDictEntity>().getPage(params),
                new QueryWrapper<SysDictEntity>()
                        .like(StringUtils.isNotBlank(name),"name", name).in(deptIdList!=null,"dept_id",deptIdList)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        for(SysDictEntity sysdictEntity : page.getRecords()){
            SysDeptEntity sysDeptEntity = sysDeptService.getById(sysdictEntity.getDeptId());
            sysdictEntity.setDeptName(sysDeptEntity.getName());
        }

        return new PageUtils(page);
    }
    @Override
    @DictFilter(subDept = true, user = false)
    public List<SysDictEntity> queryOfficePage(Map<String, Object> params) {
        String deptIdStr= (String) params.get("deptId");
        Long deptIdLong=null;
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {
            //根据部门对字典进行查看
            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            //得到父部门字典,将父字典也展示出来
            List<Long> allParentIdList = sysDeptService.queryParentIdList(deptIdLong);
            deptIdList.addAll(allParentIdList);
        }

        IPage<SysDictEntity> page = this.page(
                new Query<SysDictEntity>().getPage(params),
                new QueryWrapper<SysDictEntity>()
                        .like("name", "职名（务）").in(deptIdList!=null,"dept_id",deptIdList)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        List<SysDictEntity> sysDictEntity=page.getRecords();
        return sysDictEntity;
    }
}
